﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.DanhMucChung
{
    public partial class CustomerLookup : FormPopupBase
    {
        private readonly KhachHangBO service = new KhachHangBO();
        public KhachHang CustomerSelected;

        public CustomerLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }

      
        void InitStyle()
        {
            grvCustomerList.OptionsBehavior.Editable = false;
            grvCustomerList.OptionsView.ShowGroupPanel = false;
            grvCustomerList.OptionsView.ShowIndicator = true;

            grvCustomerList.OptionsView.ShowAutoFilterRow = true;
            grvCustomerList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvCustomerList.OptionsView.ShowGroupedColumns = false;
            grvCustomerList.OptionsMenu.EnableColumnMenu = false;
            grvCustomerList.OptionsCustomization.AllowColumnMoving = false;
            grvCustomerList.OptionsCustomization.AllowGroup = false;
            grvCustomerList.OptionsFilter.AllowFilterEditor = false;

            this.grvCustomerList.OptionsSelection.MultiSelect = false;
            this.grvCustomerList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colCode.FieldName = KhachHangColumns.MaKH.ToString();
            this.colName.FieldName = KhachHangColumns.TenKH.ToString();
            this.colAddress.FieldName = KhachHangColumns.DiaChi.ToString();
            this.colProvince.FieldName = KhachHangColumns.TinhThanh.ToString();
            this.colDistrict.FieldName = KhachHangColumns.QuanHuyen.ToString();
            this.colPhone.FieldName = KhachHangColumns.SoDT.ToString();
            this.colFax.FieldName = KhachHangColumns.SoFax.ToString();
            this.colEmail.FieldName = KhachHangColumns.Email.ToString();
        }

        void InitData()
        {
            try
            {
                this.grdCustomerList.DataSource = service.GetAllKhachHang(); 
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.CustomerSelected = (KhachHang)grvCustomerList.GetFocusedRow();
                }
                else
                {
                    this.CustomerSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvCustomerList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvCustomerList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdCustomerList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (CustomerSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvCustomerList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvCustomerList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            CustomerSelected = this.grvCustomerList.GetFocusedRow() as KhachHang;
        }

        private void grvCustomerList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
